home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 106_01.zip / STRINGS.C < prev    next >
Text File  |  1993-06-26  |  5KB  |  216 lines

  1. /*    strings.c
  2.  
  3.     Version 2.1    15-Apr-80
  4.  
  5.       this set of string functions manipulates character
  6.     arrays in a variety of useful ways. note that positions
  7.     are offset 1 from the internal workings of 'C'. Thus
  8.     the first position in a string is 1 which corresponds
  9.     to array position [0].
  10.  
  11.     NOTE: none of these routines alter the original string. 
  12.           therefore, to assign the altered version of the
  13.           string to the original string, use:
  14.  
  15.            strcpy(str,funct(s,..));
  16.                (or c = match(str,"xxx",x); in the case of match)
  17.  
  18.           since the result is stored in a work buffer (strbuf),
  19.           it should be transferred immediately to avoid being
  20.           clobbered. therefore, complex statements involving
  21.           more than one of these functions are illegal and must
  22.           be broken into steps.
  23.  
  24.     IMPORTANT: in order to communicate with the external
  25.            work buffer, all separately compiled files
  26.            should contain the following declarations
  27.            as the first order of business.
  28.  
  29.            char strbuf[256];
  30.            #define SBP    &strbuf[0]
  31.  
  32.            failure to do this will result in garbage
  33.            results from independent functions which
  34.            must refer to this common memory block.
  35.            NOTE: files created prior to 04-Apr-80
  36.            use 'work' instead of 'strbuf' - change to
  37.            'strbuf' before recompiling (and WP to SBP).
  38.  
  39.         Function Summary
  40.         ----------------
  41.  
  42. 1. delete(string,n,fpos)
  43.  
  44.     delete n characters from a copy of the given string
  45.     beginning at the first position (fpos). return the
  46.     altered string.
  47.  
  48. 2. insert(string,istring,fpos)
  49.  
  50.     insert the insertion string (istring) into a copy of
  51.     the given string beginning at the first position (fpos).
  52.     return the altered string.
  53.  
  54. 3. left(string,n)
  55.  
  56.     return the leftmost n characters from a copy of the
  57.     given string.
  58.  
  59. 4. mask(string,mstring)
  60.  
  61.     return a subset of a copy of the given string according
  62.     to the following rules:
  63.  
  64.          i) if the mask character is '?' the character is
  65.             returned.
  66.         ii) if the mask character is 'x' the character is
  67.             deleted.
  68.        iii) if the mask character is ' ' the character is
  69.             replaced by a space.
  70.  
  71.     example:
  72.  
  73.     #define TESTMASK "???  ?xx?xxx";
  74.     mask("ABCDEFGHIJKL",TESTMASK) = "ABC  FI"
  75.  
  76. 5. match(string,cstring,fpos)
  77.  
  78.     return the first position of a match of the comparison
  79.     string (cstring) within the string beginning at the first
  80.     position (fpos). return NONE (-1) if there is no match.
  81.  
  82. 6. mid(string,fpos,n)
  83.  
  84.     return a subset of n characters beginning at the first
  85.     position (fpos) of a copy of the given string.
  86.  
  87. 7. replace(string,rstring,fpos)
  88.  
  89.     replace (an equal number of) characters in a copy of the
  90.     given string with the replacement string (rstring) beginning
  91.     at the first position (fpos). return the altered string.
  92.  
  93. 8. reverse(string)
  94.  
  95.     return a copy of the given string with the characters reversed.
  96.  
  97. 9. right(string,n)
  98.  
  99.     return the rightmost n characters of a copy of the
  100.     given string.
  101. */
  102.  
  103. char strbuf[256];
  104.  
  105. #define SBP    &strbuf[0]    /* fwa of work buffer */
  106. #define EOS    '\0'        /* string terminator  */
  107. #define NONE    -1        /* negative indicator */
  108.  
  109. delete(s,n,fpos)
  110. int n, fpos;
  111. char *s;
  112. {
  113.     char *tp; tp = SBP;
  114.  
  115.     while (--fpos != 0) *tp++ = *s++; s += n;
  116.     while (*tp++ = *s++); return SBP;
  117. }
  118.  
  119. insert(s,is,fpos)
  120. int fpos;
  121. char *s, *is;
  122. {
  123.     char *tp; tp = SBP;
  124.  
  125.     while (--fpos != 0) *tp++ = *s++;
  126.     while (*tp++ = *is++); --tp;
  127.     while (*tp++ = *s++); return SBP;
  128. }
  129.     
  130. left(s,n)
  131. int n;
  132. char *s;
  133. {
  134.     char *tp; tp = SBP;
  135.  
  136.     while (n-- != 0) *tp++ = *s++;
  137.     *tp = EOS; return SBP;
  138. }
  139.  
  140. mask(s,ms)
  141. char *s, *ms;
  142. {
  143.     int n;
  144.     char *tp; tp = SBP;
  145.  
  146.     n = strlen(s);
  147.     while (n-- != 0) {
  148.     switch(*ms) {
  149.        case '?': *tp = *s;  break;
  150.        case ' ': *tp = ' '; break;
  151.        case 'x': --tp;      break;
  152.     }
  153.     tp++; s++; ms++;
  154.     }
  155.     *tp = EOS; return SBP;
  156. }
  157.       
  158. match(s,cs,fpos)
  159. char s[], cs[];
  160. int fpos;
  161. {
  162.     int i, j, k;
  163.  
  164.     for (i=--fpos; s[i] != EOS; i++) {
  165.         for (j=i, k=0; cs[k] != EOS && s[j] == cs[k]; j++, k++)
  166.             ;
  167.         if (cs[k] == EOS) return(++i);
  168.     }
  169.     return(NONE);
  170. }
  171.  
  172. mid(s,fpos,n)
  173. int fpos, n;
  174. char *s;
  175. {
  176.     char *tp; tp = SBP;
  177.  
  178.     s += --fpos;
  179.     while (n-- != 0) *tp++ = *s++;
  180.     *tp = EOS; return SBP;
  181. }
  182.  
  183. replace(s,rs,fpos)
  184. int fpos;
  185. char *s, *rs;
  186. {
  187.     int n;
  188.     char *tp; tp = SBP;
  189.  
  190.     n = strlen(rs);
  191.     while (--fpos != 0) *tp++ = *s++; s += n;
  192.     while (*tp++ = *rs++); --tp;
  193.     while (*tp++ = *s++); return SBP;
  194. }
  195.  
  196. reverse(s)
  197. char *s;
  198. {
  199.     int n;
  200.     char *tp; tp = SBP;
  201.  
  202.     n = strlen(s); s += n-1;
  203.     while (n-- != 0) *tp++ = *s--;
  204.     *tp = EOS; return SBP;
  205. }
  206.  
  207. right(s,n)
  208. int n;
  209. char *s;
  210. {
  211.     char *tp; tp = SBP;
  212.  
  213.     s += strlen(s)-n;
  214.     while (*tp++ = *s++); return SBP;
  215. }
  216.